package com.example.demo.utils;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: xumf
 * @Date: 2019/8/2 16:41
 */
public class HttpClient {

    public static void main(String[] args) {
        String url="http://zhouquanserver.ehanghai.cn/ship/ais/map/121.11416632179422/124.70120245460673/29.20003312099196/30.073758178167864/7";
//        String posturl="http://mapdeskserver.ehanghai.cn/user/login";
        try {
            String result=doGet(url);
            System.out.println(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
//        try {
//            Map<String,String> map=new HashMap<>();
//            map.put("username","admin");
//            map.put("password","admin1234");
//            doPost(posturl,map);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    /**
     * httpclient get请求
     * @param url
     * @return
     * @throws IOException
     */
    public static String doGet(String url) throws IOException {
        //创建
        HttpClientBuilder builder=HttpClientBuilder.create();
        CloseableHttpClient client=builder.build();
        //执行
        HttpUriRequest httpGet=new HttpGet(url);
        CloseableHttpResponse response=client.execute(httpGet);
        HttpEntity entity=response.getEntity();
        String result=null;
        if(entity!=null){
            result=EntityUtils.toString(entity,"utf-8");
//            System.out.println(result);
        }
        return result;
    }

    /**
     * httpclient POST请求
     * @param url
     * @param params
     * @return
     * @throws IOException
     */
    public static String doPost(String url,Map<String, String> params) throws IOException {
        HttpClientBuilder builder=HttpClientBuilder.create();
        CloseableHttpClient client=builder.build();
        HttpPost httpPost=new HttpPost(url);
        if(params!=null){
            List<NameValuePair> formparams = new ArrayList<NameValuePair>();
            for (String key : params.keySet()) {
                formparams.add(new BasicNameValuePair(key, params.get(key)));
            }
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
            httpPost.setEntity(entity);
        }
        CloseableHttpResponse response=client.execute(httpPost);
        HttpEntity entity1=response.getEntity();
        String result = null;
        if(entity1!=null){
            result=EntityUtils.toString(entity1,"utf-8");
            System.out.println(result);
        }
        return result;

    }

    /**
     * 通过json 传参
     * @param url
     * @param json
     * @throws IOException
     */
    public static String postByJson(String url,String json) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        StringEntity se = new StringEntity(json,"utf-8");
        post.setHeader("Content-Type", "application/json;charset=UTF-8");
        post.setEntity(se);
        CloseableHttpResponse response = httpClient.execute(post);
        HttpEntity entity = response.getEntity();
        String body=null;
        if(entity!=null) {
            body=EntityUtils.toString(entity);
        }
//        System.out.println(body);
        return body;
    }

    /**
     * put请求
     * @param url
     * @param json
     * @return
     * @throws IOException
     */
    public static String putByJson(String url,String token,String json) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPut post = new HttpPut(url);
        StringEntity se = new StringEntity(json,"utf-8");
        post.setHeader("Content-Type", "application/json;charset=UTF-8");
        post.setHeader("X-Subject-Token",token);
        post.setEntity(se);
        CloseableHttpResponse response = httpClient.execute(post);
        HttpEntity entity = response.getEntity();
        String body=null;
        if(entity!=null) {
            body=EntityUtils.toString(entity);
        }
        return body;
    }

    /**
     * get请求
     * @param url
     * @param token
     * @return
     * @throws IOException
     */
    public static String getByParam(String url,String token) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet get=new HttpGet(url);
        get.setHeader("X-Subject-Token",token);
        CloseableHttpResponse response = httpClient.execute(get);
        HttpEntity entity = response.getEntity();
        String body=null;
        if(entity!=null) {
            body=EntityUtils.toString(entity);
        }
        return body;
    }

}
